<?php
// 授权回调地址，将用户的UID与授权返回码存入
/*1、连接数据库 
        端口号或者地址 
        登录数据库的登录名
        登录密码
        要操作的数据库名
        */ 
$link=mysqli_connect('127.0.0.1','sfsdfsdfsdf','sdfsdfsdf','sfsdf','3306');
if(!$link){
        
}
//2、选择要操作的数据库
mysqli_select_db($link,'ali');
//3、设置交互编码
mysqli_query($link,'set names utf8');

// 分期支付分期支付分期支付分期支付 

// "trade_status":"WAIT_BUYER_PAY"

if($_POST['trade_status']=='TRADE_SUCCESS'){//交易成功
    // 保存数据操作 1、将订单状态修改为成功；2、修改平台用户的余额；3、将口碑商户扣取费率后的余额转入企业支付宝


    
    $danhao=$_POST['out_trade_no'];

    $select="select * from `jd_dingdan` where `danhao`='{$danhao}' limit 1";
    // $select="select * from `jd_kbsh` where `uid`=222";
    $arr=mysqli_query($link,$select);
    $one=mysqli_fetch_assoc($arr);

    if($one['status']==1){
        echo success;
    }else{
        $trade_no=$_POST['trade_no'];
        $buyer=$_POST['buyer_logon_id'];
        $seller=$_POST['seller_email'];
        $money=$_POST['total_amount'];    
        $seller_id=$_POST['seller_id']; 
        $buyer_id=$_POST['buyer_id'];

        // gather
        $uid=$one['uid'];
        $select="select * from `jd_gather` where `uid`={$uid} limit 1";
        // $select="select * from `jd_kbsh` where `uid`=222";
        $arr=mysqli_query($link,$select);
        $_one=mysqli_fetch_assoc($arr); 

        if($one['hk']==1){
            $yh_money   =$money*(100-$_one['up_set'])/100;
            // $total=$_one['money']+$money*(1-$_one['path3'])/100-2;
        }else{
            $yh_money   =$money*($_one['path7']-$_one['up_set'])/100;
        }
        
        // 1、将订单状态修改为成功
        $update="update `jd_dingdan` set `trade_no`='{$trade_no}',`buyer`='{$buyer}',`seller`='{$seller}',`seller_id`='{$seller_id}',`status`=1,`pay_money`={$money},`tx`=0,`yh_money`='{$yh_money}' where `danhao`='{$danhao}'";
        // UPDATE `jd_dingdan` SET `trade_no`='2017120821001004015319222317',`buyer`='173***3411',`seller`='nnxx***@162.com' WHERE `danhao`='20171208145556' ;
        $id1=mysqli_query($link,$update);
        if($id1>0){
            // 2、修改平台用户的余额；

            // $uid=$one['uid'];
            // $select="select * from `jd_gather` where `uid`={$uid} limit 1";
            // // $select="select * from `jd_kbsh` where `uid`=222";
            // $_arr=mysqli_query($link,$select);
            // $_one=mysqli_fetch_assoc($_arr);   

            if($one['hk']==1){
                $total=$_one['money']+$money*(100-$_one['up_set'])/100;
            }else{
                $total=$_one['money']+$money*($_one['path7']-$_one['up_set'])/100; 
            }
            $update  ="update `jd_dingdan` set `t_money`='{$total}' where `danhao`='{$danhao}'";
        
            mysqli_query($link,$update);

            // $total=$_one['money']+$money*(1-$_one['path3']/100);  
            // 修改平台用户余额
            $update="update `jd_gather` set `money`={$total} where `uid`={$uid}";
            $id2=mysqli_query($link,$update);
            if($id2>0){


                if($one['hk']==1){
                    //不回款不需制定
                }else{
                    // 插播：制定对应的转账表
                    $trans_money=$money*(1-$_one['path7']/100);
                    $out_biz_no ='fq'.date("YmdHis").rand(1000,9999);
                    $add="insert into `jd_trans` (`trade_no`,`uid`,`money`,`buyer_id`,`out_biz_no`) values ('{$trade_no}','{$uid}','{$trans_money}','{$buyer_id}','{$out_biz_no}')";
                    $re=mysqli_query($link,$add);
                    
                }

                // 3、制定分账信息  通过seller在口碑商户表中找到使用的授权令牌
               
                $add="insert into jd_split (`seller_id`,`money_in`,`trade_no`) values ('{$seller_id}','{$money}','{$trade_no}')";
                $id3=mysqli_query($link,$add);



                        // 4、查询是否有上级 有的话需要给上级的余额增加该笔订单的%


                        $select="select * from `jd_ptyh` where `uid`={$one[uid]} limit 1";
                        $arr=mysqli_query($link,$select);
                        $one2=mysqli_fetch_assoc($arr);  //当前用户

                        if($one2['pid']==0||$one2['pid']==1||$one2['level']>4){//没有上级或者4级以下
                            echo success;
                        }else{
                            // 查找上一级
                            $select="select * from `jd_ptyh` where `uid`={$one2[pid]} limit 1";
                            $arr=mysqli_query($link,$select);
                            $one3=mysqli_fetch_assoc($arr);  //当前用户的上级

                           
                            $shou    =$one['uid'];
                            $parent  =$one3['uid'];
                            $danhao  ='fq'.date("YmdHis").rand(1000,9999);
                            $add="insert into `jd_dividend` (`uid`,`danhao`,`trade_no`,`shou`,`parent`,`total`) values ('{$parent}','{$danhao}','{$trade_no}','{$shou}','{$parent}','{$money}')";
                            $id4=mysqli_query($link,$add);

                            if($id4>0){

                                echo success;
                            }else{
                                echo fail;
                            }

                        }     
                    
            }else{
                echo fail;
            }
        }else{
            echo fail;
        }
    }
        
}

    if($_POST){ 
        $val='---------------------POST异步通知----------------<br>';
        $val.=json_encode($_POST);
        $val.=$update;
        $val.='-------------------------------------------------<br>';
    }else{  
        $val='------------------------GET通知-------------------<br>';
        $val.=json_encode($_GET);
        $val.='-------------------------------------------------<br>';
    }  


    file_put_contents("text_pcreate.txt",$val,FILE_APPEND);


        /*


        {"gmt_create":"2017-12-25 18:16:45",
        "charset":"UTF-8",
        "seller_email":"18253658530",
        "subject":"\u51ef\u5bbe\u65af\u57fa\u8d85\u5e02",
        "sign":"IXrqmv2zFtoq6PfvHwJ6JqUF1JX6mxvDaTVNDKlijs++LHDGlDF6L+USw54YZPBdJGRbbnvluTUq7XN8QjiC6K\/hJilNpqnJ0Rc92TVmrlU6ypdzAfQCUWdM0y0yBuj5n\/hLfWoJAuZmCB1Ef24fSE\/Exoc73pgyv6KQFT7Jipw=",
        "buyer_id":"2088522562841128",
        "invoice_amount":"180.00",
        "notify_id":"f9a28fc8c6f4d7eb3cc175afb0c2956gxe",
        "fund_bill_list":"[{\"amount\":\"180.00\",
        \"fundChannel\":\"PCREDIT\"}]",    付款渠道
        "notify_type":"trade_status_sync",
        "trade_status":"TRADE_SUCCESS",
        "receipt_amount":"180.00",
        "buyer_pay_amount":"180.00",
        "app_id":"2017070507654765",
        "sign_type":"RSA",
        "seller_id":"2088422781920524",
        "gmt_payment":"2017-12-25 18:16:51",
        "notify_time":"2017-12-25 18:16:51",
        "version":"1.0",
        "out_trade_no":"20171225181641280271",
        "total_amount":"180.00",
        "trade_no":"2017122521001004120294744797",
        "auth_app_id":"2017080107988074",
        "buyer_logon_id":"131****4312",
        "point_amount":"0.00"}
        update `jd_gather` set `money`=3919.74 where `uid`=89
        -------------------------------------------------<br>



        */


